﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using PictureProcessing.Comm;


namespace PictureProcessing
{
    public partial class FrmDataProcessing : Form
    {
        private List<string> dirslist;//获取图片集合
        private List<string> dirslistafter;//处理后的图片
        private int iamgeindex = 0;//当前图片索引
        private int maxindex = 0;//图片最大索引
        public FrmDataProcessing()
        {
            InitializeComponent();
        }

        private void FrmDataProcessing_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            dateTimePicker1.ShowUpDown = true;

            dirslist = new List<string>();
            dirslistafter = new List<string>();
            dirslist.Clear();
            dirslistafter.Clear();


        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            this.txtUrl.Text = path.SelectedPath;

            if (this.txtUrl.Text.Length > 0)
            {
                //读取相片
                string imgtype = "*.BMP|*.JPG|*.GIF|*.PNG";
                string[] ImageType = imgtype.Split('|');
                for (int i = 0; i < ImageType.Length; i++)
                {
                    string[] strurl = Directory.GetFiles(this.txtUrl.Text, ImageType[i]);
                    if (strurl.Length > 0)
                    {

                        foreach (string dir in strurl)
                        {
                            //全名路径包含后缀
                            string fullname = dir;
                            dirslist.Add(dir);
                        }
                    }

                }
                if (dirslist.Count > 0)
                {
                    maxindex = dirslist.Count;
                    //this.pictureBox1.Image = Image.FromFile(dirslist[0], false);
                    this.pictureBox1.ImageLocation = dirslist[0].ToString();
                    this.txtFileName.Text = DateHandlecs.GetFileName(dirslist[0].ToString());
                }

            }


        }

        private void btnAnalysis_Click(object sender, EventArgs e)
        {
            bool dataBackUp = IsBackup.Checked;//数据是否备份 true 备份 false 不备份
            bool report = IsReport.Checked;
            bool Rename = IsRename.Checked;
            string PointPosition = txtPointPosition.Text;
            string CameraNumber = txtCameraNumber.Text;
            string dataUrl = txtUrl.Text;
            if (Rename == true)
            {

                if (string.IsNullOrEmpty(PointPosition) || string.IsNullOrEmpty(CameraNumber))
                {
                    MessageBox.Show("【布设点位编号】或【相机编号】不能为空!");
                    return;
                }
            }

            if (string.IsNullOrEmpty(dataUrl))
            {
                MessageBox.Show("请选择需要分析的文件路劲");
                return;
            }
            //文件备份
            if (dataBackUp == true)
            {
                DateHandlecs.CopyFolder(dataUrl, dataUrl + "\\backup");
            }

            //重新命名
            if (Rename == true)
            {

                string picurl = this.pictureBox1.ImageLocation;
                string oldname = DateHandlecs.GetFileName(picurl);
                string NewName = PointPosition + "-" + CameraNumber + "-" + oldname;
                string NewUrl = picurl.Replace(oldname, NewName);

                DirFile.Rename(picurl, NewUrl);
            }

            if (report == true)
            {
                //分析文件保存信息到imageinfo.csv
                string ObjectClass = txtObjectClass.Text;
                string ObjectName = txtObjectName.Text;
                string ObjectNum = txtObjectNum.Text;
                string Sex = txtSex.Text;
                string Remarks = txtRemarks.Text;
                string savePath = txtUrl.Text + "\\imageinfo.csv";


                //创建表
                DataTable dt = new DataTable();
                //1、添加列  对象类别  对象名称
                dt.Columns.Add("ObjectClass", typeof(string)); //数据类型为 文本
                dt.Columns.Add("ObjectName", typeof(string)); //数据类型为 文本

                //3、添加数据行 
                DataRow dr2 = dt.NewRow();
                dr2["ObjectClass"] = ObjectClass; //通过索引赋值
                dr2["ObjectName"] = ObjectName;

                dt.Rows.Add(dr2);
                //dt2csv(DataTable dt, string strFilePath, string tableheader, string columname)

                CsvHelper.dt2csv(dt, savePath, "数据保存", "对象类别,对象名称");
            }



          


        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            //iamgeindex//当前图片索引
            //maxindex//图片最大索引
            if (maxindex > 0)
            {
                if (maxindex == iamgeindex + 1)
                {
                    MessageBox.Show("已是最后一张图片！");
                    return;
                }
                iamgeindex = iamgeindex + 1;
                this.pictureBox1.ImageLocation = dirslist[iamgeindex].ToString();
                this.txtFileName.Text = DateHandlecs.GetFileName(dirslist[iamgeindex].ToString());

            }
        }

        private void btnForward_Click(object sender, EventArgs e)
        {
            if (maxindex > 0)
            {
                if (0 > iamgeindex - 1)
                {
                    MessageBox.Show("已是第一张图片！");
                    return;
                }
                iamgeindex = iamgeindex - 1;
                this.pictureBox1.ImageLocation = dirslist[iamgeindex].ToString();
                this.txtFileName.Text = DateHandlecs.GetFileName(dirslist[iamgeindex].ToString());

            }
        }

        private void btnBigPicture_Click(object sender, EventArgs e)
        {
            try
            {
                string filename = pictureBox1.ImageLocation;
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = filename;
                process.StartInfo.Arguments = "rundll32.exe C://WINDOWS//system32//shimgvw.dll";
                process.StartInfo.UseShellExecute = true;
                process.Start();

            }
            catch (Exception ex)
            {
                MessageBox.Show("打开图片失败:" + ex.Message);
            }
        }

        private void txtPointPosition_Click(object sender, EventArgs e)
        {

        }
    }


}
